﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Configurations.Controller;

namespace Pixysoft.Framework.Configurations.Core
{
    class XmlTree : IXmlTree
    {
        List<IXmlNode> nodes = new List<IXmlNode>();


        public IXmlNodeCollection Node
        {
            get
            {
                return new XmlNodeCollection(nodes);
            }
        }

        public IXmlNodesCollection Nodes
        {
            get
            {
                List<IXmlNode> list = new List<IXmlNode>();
                foreach (IXmlNode node in nodes)
                    list.Add(node);
                return new XmlNodesCollection(nodes, list);
            }
        }


        public void Save()
        {
            string filename = StringHelper.default_config_basepath + StringHelper.default_config_filename;

            Save(filename);
        }

        public void Save(string filename)
        {
            XmlNodeController.Instance.Save(filename, this);
        }


        public List<string> Serialize()
        {
            return Serialize(false);
        }

        public List<string> Serialize(bool indent)
        {
            return XmlNodeController.Instance.Serialize(this, indent);
        }

        public List<T> Serialize<T>()
        {
            List<T> list = new List<T>();

            foreach (string serialize in Serialize())
            {
                list.Add(Pixysoft.IO.XmlImporter.Instance.XmlDeserialize<T>(serialize));
            }

            return list;
        }


        public string SerializeAll()
        {
            return SerializeAll(false);
        }

        public string SerializeAll(bool indent)
        {
            return XmlNodeController.Instance.SerializeAll(this, indent);
        }        
    }
}
